#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：back 
@File    ：complaint_svc.py
@IDE     ：PyCharm 
@Author  ：七分是你
@Date    ：2024/8/12 下午4:14 
@explain : 
"""
import math

from app import dao
from app import utils
from app.types import appproto


class ComplaintService:
    @classmethod
    def get_complaint_list(cls, uid: int = 0, status: str = '', key_type: str = '', keyword: str = '',
                           page: int = 1,
                           page_size: int = 10, ) -> utils.HttpResponse:
        where = {}
        if uid != 0:
            where['uid'] = uid
        if status != '':
            where['status'] = status
        if key_type != '' and keyword != '':
            where[key_type] = '%' + keyword + '%'
        total, result = dao.ComplaintQueryDao.list(page=page, page_size=page_size, **where)
        if total == 0:
            return utils.ResponseSuccess(None)
        # 格式化数据
        records_list = []

        for record in result:
            parent_records = {'id': record.id, 'complaint': record.complaint, 'uid': record.uid,
                              'complainttype': record.complainttype,
                              'content': record.content, 'project': record.project,
                              'zbdwname': record.zbdwname, 'signcontract': record.signcontract,
                              'contractattachments': record.contractattachments,
                              'attendance': record.attendance, 'totalattendance': record.totalattendance,
                              'workt': record.workt, 'payoff': record.payoff,
                              'amount': record.amount, 'arrearsmonths': record.arrearsmonths,
                              'arrearsamount': record.arrearsamount,
                              'unpaidwagest': record.unpaidwagest, 'wagearrearspep': record.wagearrearspep,
                              'totalamountwages': record.totalamountwages,
                              'name': record.name, 'card': record.card, 'phone': record.phone, 'nation': record.nation,
                              'area': record.area, 'address': record.address, 'reportfiles': record.reportfiles,
                              'addtime': utils.StringUtil.format_timestamp_to_str(record.addtime),
                              'status': record.status, 'reply_content': record.reply_content,
                              'reply_time': utils.StringUtil.format_timestamp_to_str(
                                  record.reply_time) if record.reply_time else '正在受理中',

                              }
            records_list.append(parent_records)
        res = {'items': records_list, 'total': total, 'current_page': page, 'pagesize': page_size,
               'total_page': math.ceil(total / page_size)}
        return utils.ResponseSuccess(res)

    @classmethod
    def add(cls, data) -> utils.HttpResponse:
        pass

    @classmethod
    def update(cls, data: appproto.ComplaintData) -> utils.HttpResponse:
        article = dao.ComplaintOperateDao.updata(data.model_dump())
        return utils.ResponseSuccess('回复成功')

    @classmethod
    def guestbook_del(cls, ids) -> utils.HttpResponse:
        pass
